<?php
session_start();
error_reporting(E_ALL & ~(E_STRICT|E_NOTICE));

      //微信API初始验证
      if($_GET['echostr']){
        check_weixin_api();
        die();
      }
      //路由跳转
      if ($_GET['a']=="getinfo"){       
        getinfo();die();
      }elseif($_GET['a']=="getcode"){
        getcode();die();       
      }else{       
        getinfo();die();       
      }

  function getinfo(){ 
       $appid = 'wx7486af3dbd72e41d';               
       $appsecret   = '1e493cc067db3cebe5382c2728b0c890';    
       $redirect = urlencode('http://weixin.job.waijiao.org.cn/test.php?a=getcode');    
       $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";    
       echo '<meta http-equiv="Refresh" content="0; url='.$url.'" /> ';  
  }

  function getcode(){
          $appid = 'wx7486af3dbd72e41d';
          $appsecret   = '1e493cc067db3cebe5382c2728b0c890';
          $code = $_GET['code'];
          $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
          $data = array();
          $res = http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
          echo '<meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0" />
          ';
          $resArr = json_decode($res);
          $access_token = $resArr->access_token;
          $openid = $resArr->openid;
          $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
          $res2 =  http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));
          $resArr2 = json_decode($res2);
          $nickname  = $resArr2->nickname;
          $pic       = $resArr2->headimgurl;
          $sex       = $resArr2->sex;
          $openid    = $resArr2->openid;

          //此处给用户设置session，以便于网页跳转的时候把需要的参数带过去
          $_SESSION['id']=$appid;
          $_SESSION['nickname']=$nickname;
          $_SESSION['pic']=$pic;
          $_SESSION['sex']=$sex;
          $_SESSION['openid']=$openid;

          //此处是需要二次跳转的代码，改变url地址就可以连接到任何地方.
          echo '<meta http-equiv="Refresh" content="0;url=get_message.php" /> '; 
  }  

  function check_weixin_api(){

      define(TOKEN,'weixin1234');
      $timestamp = $_GET['timestamp'];
      $nonce     = $_GET['nonce'];
      $token     = TOKEN;
      $signature = $_GET['signature'];
      $tmpArr = array($timestamp,$nonce,$token);
      sort($tmpArr,SORT_STRING);
      $tmpStr = sha1(implode($tmpArr));
      if ($tmpStr == $signature  && $_GET['echostr']){
          echo $_GET['echostr'];
      }else{
          //echo '没有Echo Str ，FALSE';
          echo "FALSE";     
      }

   }

  function http($url, $params, $method = 'GET', $header = array(), $multi = false){
      $opts = array(
              CURLOPT_TIMEOUT        => 30,
              CURLOPT_RETURNTRANSFER => 1,
              CURLOPT_SSL_VERIFYPEER => false,
              CURLOPT_SSL_VERIFYHOST => false,
              CURLOPT_HTTPHEADER     => $header
      );
      /* 根据请求类型设置特定参数 */
      switch(strtoupper($method)){
          case 'GET':
              $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
              break;
          case 'POST':
              //判断是否传输文件
              $params = $multi ? $params : http_build_query($params);
              $opts[CURLOPT_URL] = $url;
              $opts[CURLOPT_POST] = 1;
              $opts[CURLOPT_POSTFIELDS] = $params;
              break;
          default:
              throw new Exception('不支持的请求方式！');
      }
      /* 初始化并执行curl请求 */
      $ch = curl_init();
      curl_setopt_array($ch, $opts);
      $data  = curl_exec($ch);
      $error = curl_error($ch); 
      curl_close($ch);
      if($error) throw new Exception('请求发生错误：' . $error);
      return  $data;
  }

?>